let socket = io();
let map1 = null;
let AMAP = null;
window._AMapSecurityConfig = {
    securityJsCode:'fb280dc459e83ebc21b5b1016354d568',
}
AMapLoader.load({
    "key": "45ab9b2058971e1311125a44c16637ab",
    "version": "2.0",
    "plugins": ['AMap.DragRoute', 'AMap.Driving'],
    "Loca": {
        "version": '2.0'
    },
}).then((AMap) => {
    AMAP = AMap
    const opt = {
        center: [114.058022, 22.543043],
        zoom: 13
    }
    map1 = new AMap.Map('map1', opt);
    let points = []
    map1.on('click', (e) => {
        if (points.length <= 1) {
            points.push(e.lnglat)
        }
        if (points.length !== 2) {
            return
        }
        pathPlanning(points)
        points = []
    })
}).catch((e) => {
    console.error(e);  //加载错误提示
});


function pathPlanning(points) {
    console.log('pathPlanning', points)
    map1.clearMap()
    pathPlanning2(points)
    pathPlanning1(points)
}


function pathPlanning1(points) {
    let driving = new AMap.Driving({policy: 2})
    driving.search(points[0], points[1], function (status, result) {
        if (status === 'complete') {
            console.log('高德距离:', result.routes[0].distance)
            let path = []
            for (let item of result.routes[0].steps) {
                for (let p of item.path) {
                    path.push(p)
                }
            }
            let polyline = new AMAP.Polyline({
                path: path,
                strokeWeight: 6,
                strokeColor: 'red',
                lineJoin: 'round',
                showDir: true
            });
            map1.add(polyline);
        }
    })
}

function pathPlanning2(points) {
    let arr = points.map(item => GCJ02ToWGS84(item.lng, item.lat))
    socket.emit('path_planning', {
        startPoint: {
            lng: arr[0][0],
            lat: arr[0][1]
        },
        endPoint: {
            lng: arr[1][0],
            lat: arr[1][1]
        }
    });
}


socket.on('path_planning', function (msg) {
    if (msg.status === 'error') {
        console.log('路径规划失败:', msg)
        return
    }
    console.log('路径规划距离:', msg.distance, "用时:", msg.use_time)
    let path = msg.path.map(item => WGS84ToGCJ02(item.lng, item.lat)).map(item => new AMap.LngLat(item[0], item[1]))
    let polyline = new AMAP.Polyline({
        path: path,
        strokeWeight: 6,
        strokeColor: 'blue',
        lineJoin: 'round',
        showDir: true
    });
    map1.add(polyline);
});


/**
 * WGS84转GCj02
 */
function WGS84ToGCJ02(lng, lat) {
    const PI = 3.1415926535897932384626;
    const a = 6378245.0;
    const ee = 0.00669342162296594323;
    let dlat = transformLat(lng - 105.0, lat - 35.0);
    let dlng = transformLng(lng - 105.0, lat - 35.0);
    let radlat = lat / 180.0 * PI;
    let magic = Math.sin(radlat);
    magic = 1 - ee * magic * magic;
    let sqrtmagic = Math.sqrt(magic);
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
    dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
    let mglat = lat + dlat;
    let mglng = lng + dlng;
    return [mglng, mglat]
}

/**
 * GCJ02 转换为 WGS84
 */
function GCJ02ToWGS84(lng, lat) {
    const PI = 3.1415926535897932384626;
    const a = 6378245.0;
    const ee = 0.00669342162296594323;
    let dlat = transformLat(lng - 105.0, lat - 35.0);
    let dlng = transformLng(lng - 105.0, lat - 35.0);
    let radlat = lat / 180.0 * PI;
    let magic = Math.sin(radlat);
    magic = 1 - ee * magic * magic;
    let sqrtmagic = Math.sqrt(magic);
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
    dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
    let mglat = lat + dlat;
    let mglng = lng + dlng;
    return [lng * 2 - mglng, lat * 2 - mglat]
}


function transformLat(lng, lat) {
    const PI = 3.1415926535897932384626;
    let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
    return ret
}

function transformLng(lng, lat) {
    const PI = 3.1415926535897932384626;
    let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
    return ret
}